/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Networking;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ksweeney12
 */
public class UDPServer{
    String group;
    DatagramSocket theSocket;
    InetAddress theGroupAddress;
    
    ArrayList<Client> clients;
    
    private Listening listener;
    private Sending sender;
    
    SendablePackage sendingPackage;
    Package thePackage;
    
    public UDPServer(String groupIP) throws IOException{
        group=new String(groupIP);
        theGroupAddress=InetAddress.getByName(group);
        theSocket=new DatagramSocket(5555);
        thePackage=new j();
        clients=new ArrayList<Client>();
        listener=new Listening();
    }
    
    public void start(){
        listener.start();
        sender.start();
    }
    
    public void changeSendableData(Package object) throws IOException{
        if (!object.equals(thePackage)) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
            ObjectOutputStream out = new ObjectOutputStream(baos);
            out.writeObject(object);
            out.close();
            sendingPackage=new SendablePackage(baos.toByteArray());
        }
    }
    
    public void changeSendableData(Package object, int bytes) throws IOException{
        if (!object.equals(thePackage)) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes);
            ObjectOutputStream out = new ObjectOutputStream(baos);
            out.writeObject(object);
            out.close();
            sendingPackage = new SendablePackage(baos.toByteArray());
        }
    }
    
    public void startGameMode(){
        listener.setGameMode();
    }
    
    
    private class Listening extends Thread{
        
        String question;
        byte[] questionData;
        String answer;
        byte[] answerData;
        DatagramPacket questionPacket;
        DatagramPacket answerPacket;
        
        boolean getClientMode;
        boolean gameMode;
        
        public Listening(){
            question="Hello, is anyone there?";
            questionData=question.getBytes();
            questionPacket=new DatagramPacket(questionData,questionData.length);
            answer="Yes, I am here";
            answerData=answer.getBytes();
            answerPacket=new DatagramPacket(answerData, answerData.length);
            getClientMode=true;
        }
        
        @Override
        public void run(){
            while (getClientMode) {
                synchronized (this) {
                    try {
                        theSocket.receive(questionPacket);
                        String response=new String(questionPacket.getData());
                        if(response.equals(question)){
                            InetAddress q=questionPacket.getAddress();
                            int port=questionPacket.getPort();
                            answerPacket.setAddress(q);
                            answerPacket.setPort(port);
                            theSocket.send(answerPacket);
                            clients.add(new Client(port, q));
                        } 
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            while(gameMode){
                synchronized(this){
                    try{
                        
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        
        public void setGameMode(){
            synchronized(this){
                getClientMode=false;
                gameMode=true;
            }
        }
    }
    
    private class Sending extends Thread{
        
        @Override
        public void run(){
            
        }
    }
    
    private class Client{
        int port;
        InetAddress address;
        
        public Client(int port, InetAddress address){
            this.port=port;
            this.address=address;
        }
    }
    //Used for example/default package
    private class j extends Package{

        @Override
        public boolean equals(Object p) {
            return false;
        }

        @Override
        public int hashCode() {
            return 3;
        }
        
        public j(){
            
        }
        
    }
}
